package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysElectricVehBrand;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.model.BoxVo;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysElectricVehBrandService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sys/electricVehBrand")
@Api(tags = "电动车品牌管理")
public class SysElectricVehBrandController {


    @Autowired
    private SysElectricVehBrandService sysElectricVehBrandService;


    @ApiOperation(value = "分页查询电动车品牌")
    @GetMapping
    public JsonResult<Page<SysElectricVehBrand>> findByPage(Page<SysElectricVehBrand> page, String name) {
        page.addOrder(OrderItem.desc("created"));
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<SysElectricVehBrand> electricVehBrandIPage = sysElectricVehBrandService.page(page,
                new LambdaQueryWrapper<SysElectricVehBrand>()
                        .eq(shopId != 1, SysElectricVehBrand::getShopId, shopId)
                        .like(!StringUtils.isEmpty(name), SysElectricVehBrand::getName, name));
        return JsonResult.ok(electricVehBrandIPage);
    }

    @ApiOperation(value = "根据id查询电动车品牌")
    @GetMapping("{id}")
    public JsonResult<SysElectricVehBrand> getElectricVehBrand(@PathVariable("id") Long id) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<SysElectricVehBrand> vehBrandList = sysElectricVehBrandService.list(queryById(id, shopId));
        SysElectricVehBrand electricVehBrand = null;
        if (!CollectionUtils.isEmpty(vehBrandList)){
            electricVehBrand = vehBrandList.get(0);
        }
        return JsonResult.ok(electricVehBrand);
    }

    @ApiOperation(value = "查询电动车品牌下拉框")
    @GetMapping("getBox")
    public JsonResult<List<BoxVo>> getBox(Byte status) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<SysElectricVehBrand> list = sysElectricVehBrandService
                .list(new LambdaQueryWrapper<SysElectricVehBrand>()
                        .eq(shopId != 1, SysElectricVehBrand::getShopId, shopId)
                        .eq(status != null, SysElectricVehBrand::getStatus, status));
        List<BoxVo> result = new ArrayList<>(list.size());
        if (!CollectionUtils.isEmpty(list)) {
            for (SysElectricVehBrand electricVehBrand : list) {
                result.add(new BoxVo(electricVehBrand.getId(), electricVehBrand.getName()));
            }
        }
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "查询电池品牌下拉框")
    @GetMapping("getBatteryBox")
    public JsonResult<List<BoxVo>> getBatteryBox() {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<SysElectricVehBrand> list = sysElectricVehBrandService
                .list(new LambdaQueryWrapper<SysElectricVehBrand>()
                        .eq(shopId != 1, SysElectricVehBrand::getShopId, shopId)
                        .eq(SysElectricVehBrand::getStatus, 1)
                        .eq(SysElectricVehBrand::getType, 2)
                );
        List<BoxVo> result = new ArrayList<>(list.size());
        if (!CollectionUtils.isEmpty(list)) {
            for (SysElectricVehBrand electricVehBrand : list) {
                result.add(new BoxVo(electricVehBrand.getId(), electricVehBrand.getName()));
            }
        }
        return JsonResult.ok(result);
    }


    @ApiOperation(value = "设置品牌状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody SysElectricVehBrand electricVehBrand) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Long id = electricVehBrand.getId();
        boolean save = sysElectricVehBrandService.update(electricVehBrand, queryById(id, shopId));
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "添加电动车品牌")
    @PostMapping
    public JsonResult addElectricVehBrand(@RequestBody SysElectricVehBrand electricVehBrand) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        electricVehBrand.setShopId(shopId);
        SysElectricVehBrand electricVehBrandDB = initDB(electricVehBrand);
        boolean save = sysElectricVehBrandService.save(electricVehBrandDB);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改电动车品牌")
    @PutMapping
    public JsonResult updateElectricVehBrand(@RequestBody SysElectricVehBrand electricVehBrand) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Long id = electricVehBrand.getId();
        SysElectricVehBrand electricVehBrandDB = initDB(electricVehBrand);
        boolean update = sysElectricVehBrandService.update(electricVehBrandDB, queryById(id, shopId));
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除电动车品牌")
    @DeleteMapping("{id}")
    public JsonResult delElectricVehBrand(@PathVariable("id") Long id) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        boolean remove = sysElectricVehBrandService.remove(queryById(id, shopId));
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    /***
     * 初始化校验参数
     * @param electricVehBrand
     * @return
     */
    private SysElectricVehBrand initDB(SysElectricVehBrand electricVehBrand) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        int count = sysElectricVehBrandService.count(new LambdaQueryWrapper<SysElectricVehBrand>()
                .eq(SysElectricVehBrand::getShopId, shopId)
                .eq(SysElectricVehBrand::getName, electricVehBrand.getName()));
        if (electricVehBrand.getId() == null) {
            if (count > 0) {
                throw new MyException("电动车品牌已存在!");
            }
            electricVehBrand.setId(IdWorker.getId());
            electricVehBrand.setCreated(new Date());
            electricVehBrand.setStatus((byte) 1);
            electricVehBrand.setCreateBy(SecurityUtils.getUserId());
            return electricVehBrand;
        }
        if (count > 1) {
            throw new MyException("电动车品牌已存在!");
        }
        electricVehBrand.setModifyBy(SecurityUtils.getUserId());
        return electricVehBrand;
    }

    private LambdaQueryWrapper<SysElectricVehBrand> queryById(Long id, Long shopId){
      return new LambdaQueryWrapper<SysElectricVehBrand>()
                .eq(SysElectricVehBrand::getShopId, shopId)
                .eq(SysElectricVehBrand::getId, id);
    }


}
